package com.simple.crawler.core.listener;

import com.simple.crawler.aop.AopHelper;
import com.simple.crawler.core.Crawler;
import com.simple.crawler.core.CrawlerContext;
import com.simple.crawler.core.TaskExecutor;
import com.simple.crawler.core.task.MultipleTask;
import com.simple.crawler.utils.StringUtils;
import net.sf.cglib.proxy.MethodProxy;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
public class MultipleTaskListener implements TaskListener {

    @Override
    public void after(Object o, Method method, Object[] objects, MethodProxy methodProxy) {
        MultipleTask task = (MultipleTask) o;
        if (StringUtils.isBlank(task.getNextPageUrl())) {
            logger.debug("没有下一页了");
            return;
        }
        Crawler crawler = CrawlerContext.getCrawler(task.getCrawlerId());
        if (crawler == null) {
            return;
        }
        logger.debug("多页任务进行翻页操作进行翻页操作");
        TaskExecutor.addTask(AopHelper.getAopHelper().initObj(task.getClass(), new Object[]{crawler.getId(), task.getIndex(), task.getNextPageUrl()}));
    }

}
